package com.sinodata.bsm.cicp.collector.sybase;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.AbstractJDBCCollector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.common.constants.SeparatorConstants;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * 
 * Configs.java
 * <p>
 *
 * @author 管普贵 guanpg@uni-mon.com
 * @version 2.0 
 * @Date Jan 8, 2010
 */
public class Configs extends AbstractJDBCCollector {

    private static final Logger logger = Logger.getLogger(Configs.class);

    /**
     * 采集方法
     * 
     * @param pParmeters
     *            Sybase Configs资源的属性值数组: 数据库配置信息
     * @return session,
     */
    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {

        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        ResPropertyValue propertyValueX = new ResPropertyValue();
        try {
            String[] propMetas = getPropMeta();
            //        String serverName = this.getParamMap().get("服务器名");
            String serverName = this.getParamMap().get("ServerName");

            String sql = "select c1.name name, c1.comment comment, c2.defvalue defvalue, isnull(c2.value2,'N/A') value2," + " c2.minimum_value minimum_value, c2.maximum_value maximum_value from master.dbo.sysconfigures c1," + " master.dbo.syscurconfigs c2 where c1.config = c2.config";

            String[] rs = this.getStrings(serverName, sql, ip);
            ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
            for (int i = 0; i < propMetas.length; i++) {
                propValues[i] = ResPropertyValue.createPropValue(rs[i], propMetas[i]);
            }
            propertyValueX.setValues(propValues);
            values.add(propertyValueX);
        } catch (Exception ex) {
            logger.error("failed to collect sybase config", ex);
            throw new CollectorException(ex);
        }
        return values;
    }

    private String[] getStrings(String serverName, String sql, String ip) throws CollectorException {
        String[] propValArr = new String[1];
        try {

            ResultSet rst = this.getCachedResultSet("sybase", ip, serverName, sql);
            if (rst != null) {
                StringBuffer configureInfo = new StringBuffer();
                // 名称,描述,默认值,最小值,最大值,配置值
                //                configureInfo.append("Name").append(SeparatorConstants.COL_SEPARATOR);//名称
                //                configureInfo.append("Describe").append(SeparatorConstants.COL_SEPARATOR);//描述
                //                configureInfo.append("Default").append(SeparatorConstants.COL_SEPARATOR);//默认值
                //                configureInfo.append("Min").append(SeparatorConstants.COL_SEPARATOR);//最小值
                //                configureInfo.append("Max").append(SeparatorConstants.COL_SEPARATOR);//最大值
                //                configureInfo.append("Config").append(SeparatorConstants.COL_SEPARATOR);//配置值
                //                configureInfo.append(SeparatorConstants.LIN_SEPARATOR);
                while (rst.next()) {
                    String line = getTabLineStr(rst, 6);
                    configureInfo.append(line).append(SeparatorConstants.LIN_SEPARATOR);
                }
                String configureInfostr = configureInfo.toString();

                propValArr[0] = configureInfostr;
            } else {
                return null;
            }

        } catch (Exception ex) {
            logger.error("while connect database : ", ex);
            throw new CollectorException(ex);
        }

        return propValArr;
    }

    private String getTabLineStr(ResultSet rst, int colNum) throws CollectorException {
        StringBuffer tabLineStr = new StringBuffer();
        try {
            for (int i = 1; i <= colNum; i++) {
                String val = rst.getString(i);
                if (val == null || val.trim().length() == 0) {
                    val = " ";
                } else
                    val = val.trim();
                tabLineStr.append(val).append(SeparatorConstants.COL_SEPARATOR);
            }
        } catch (Exception e) {
            logger.error("failed to get TabLineStr", e);
            throw new CollectorException(e);
        }
        return tabLineStr.toString();
    }

    @Override
    public String[] getParamMeta() {
        return new String[] { "ServerName" };
    }

    @Override
    public String[] getPropMeta() {
        //        return new String[] { "配置信息列表" };
        return new String[] { "ConfigInfoList" };
    }

}
